package com.ruoyi.yuwen.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.newAdd.returnResult.Data;
import com.ruoyi.common.newAdd.returnResult.Res;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.yuwen.domain.User;
import com.ruoyi.yuwen.domain.UserGoods;
import com.ruoyi.yuwen.service.IUserGoodsService;
import com.ruoyi.yuwen.service.IUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.yuwen.domain.Goods;
import com.ruoyi.yuwen.service.IGoodsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品Controller
 * 
 * @author ruoyi
 * @date 2023-02-27
 */
@RestController
@RequestMapping("/yuwen/goods")
public class GoodsController extends BaseController
{
    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IUserGoodsService userGoodsService;

    /**
     * 查询产品列表
     */
    @PreAuthorize("@ss.hasPermi('yuwen:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(Goods goods)
    {
        startPage();
        List<Goods> list = goodsService.selectGoodsList(goods);
        return getDataTable(list);
    }

    /**
     * 导出产品列表
     */
    @PreAuthorize("@ss.hasPermi('yuwen:goods:export')")
    @Log(title = "产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Goods goods)
    {
        List<Goods> list = goodsService.selectGoodsList(goods);
        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
        util.exportExcel(response, list, "产品数据");
    }

    /**
     * 获取产品详细信息
     */
    @PreAuthorize("@ss.hasPermi('yuwen:goods:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(goodsService.selectGoodsById(id));
    }

    /**
     * 新增产品
     */
    @PreAuthorize("@ss.hasPermi('yuwen:goods:add')")
    @Log(title = "产品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Goods goods)
    {
        goodsService.insertGoods(goods);
        /** 新增产品库存 */
        User temp = new User();
        temp.setUserLevel(101l);
        List<User> userList = userService.selectUserList(temp);
        if (userList.size()>0){
            for (User user : userList){
                UserGoods ug = new UserGoods();
                ug.setGoodsId(goods.getId());
                ug.setGoodsName(goods.getName());
                ug.setUserId(user.getId());
                ug.setUserName(user.getName());
                ug.setStock(999999999l);
                userGoodsService.insertUserGoods(ug);
            }
        }
        return toAjax(1);
    }

    /**
     * 修改产品
     */
    @PreAuthorize("@ss.hasPermi('yuwen:goods:edit')")
    @Log(title = "产品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Goods goods)
    {
        return toAjax(goodsService.updateGoods(goods));
    }

    /**
     * 删除产品
     */
    @PreAuthorize("@ss.hasPermi('yuwen:goods:remove')")
    @Log(title = "产品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        userGoodsService.deleteUserGoodsByGoodsIds(ids);
        return toAjax(goodsService.deleteGoodsByIds(ids));
    }


    /** 前端接口 **************************************************************************************************** */
    /** 前端接口 **************************************************************************************************** */
    /** 前端接口 **************************************************************************************************** */


    /**
     * 商品列表
     */
    @GetMapping("/front/goodsList")
    public Res goodsList(){

        Goods goods = new Goods();

        startPage();
        List<Goods> list = goodsService.selectGoodsList(goods);
        Long total = new PageInfo(list).getTotal();

        Data data = Data.ok().put("total",total).put("list",list);
        return Res.ok().put("data", data);
    }

    /**
     * 商品详情
     */
    @GetMapping("/front/goodsInfo")
    public Res goodsInfo(@RequestParam("id")Long id){

        Goods goods = goodsService.selectGoodsById(id);

        Data data = Data.ok().put("goods",goods);
        return Res.ok().put("data", data);
    }
}
